package com.lgmshare.k3.ui.base;

import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.StringRes;
import androidx.appcompat.widget.Toolbar;

import com.lgmshare.component.app.LaraActivity;
import com.lgmshare.component.network.HttpRequestClient;
import com.lgmshare.component.utils.StatusBarUtils;
import com.lgmshare.component.utils.UIUtils;
import com.lgmshare.k3.R;
import com.lgmshare.k3.ui.dialog.CommonDialog;
import com.lgmshare.k3.ui.dialog.LoadingDialog;
import com.lgmshare.k3.utils.AnalyticsUtils;

import butterknife.ButterKnife;

/**
 * @author lim
 * @description: TODO
 * @email lgmshare@gmail.com
 * @datetime 2017/3/14 14:46
 */
public abstract class BaseActivity extends LaraActivity {

    protected Toolbar mToolbar;

    /**
     * 加载等待框
     */
    private LoadingDialog mLoadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //状态栏模式
        StatusBarUtils.setLightMode(getActivity());
        super.onCreate(savedInstanceState);
    }

    protected boolean showHasActionBar() {
        return true;
    }

    protected boolean showHomeMenuItem() {
        return true;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        if (showHasActionBar()) {
            mToolbar = findViewById(R.id.toolbar);
            setSupportActionBar(mToolbar);
            if (showHomeMenuItem()) {
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        AnalyticsUtils.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        AnalyticsUtils.onPause(this);
    }

    @Override
    protected void onStop() {
        dismissProgressDialog();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        HttpRequestClient.cancel(this);
        dismissProgressDialog();
        super.onDestroy();
    }

    /**
     * 使用框架默认的toolbar.xml配置设置title
     * 默认返回按钮
     *
     * @param title
     */
    protected void setToolbarTitle(CharSequence title) {
        mToolbar.setTitle(title);
    }

    /**
     * 使用框架默认的toolbar.xml配置设置title
     * 默认返回按钮
     *
     * @param title
     */
    protected void setToolbarTitle(@StringRes int title) {
        mToolbar.setTitle(title);
    }

    /**
     * 提示信息
     *
     * @param res
     */
    protected void showToast(int res) {
        showToast(getString(res));
    }

    /**
     * 提示信息
     *
     * @param msg
     */
    protected void showToast(String msg) {
        if (!isFinishing() && !TextUtils.isEmpty(msg)) {
            UIUtils.showToast(msg);
        }
    }

    /**
     * dialog提示信息
     *
     * @param res
     */
    protected void showDialogToast(int res) {
        showDialogToast(getString(res));
    }

    /**
     * dialog提示信息
     *
     * @param msg
     */
    protected void showDialogToast(String msg) {
        CommonDialog dialog = new CommonDialog(this);
        dialog.setContent(msg);
        dialog.show();
    }

    /**
     * 显示加载框
     */
    protected void showProgressDialog() {
        showProgressDialog("");
    }

    /**
     * 显示加载框
     *
     * @param message
     */
    protected void showProgressDialog(CharSequence message) {
        showProgressDialog(message, false);
    }

    /**
     * 显示加载框
     *
     * @param message
     * @param cancelable 是否可以取消
     */
    protected void showProgressDialog(CharSequence message, boolean cancelable) {
        if (!isFinishing()) {
            if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
                return;
            }
            mLoadingDialog = new LoadingDialog(this);
            mLoadingDialog.setMessage(message);
            mLoadingDialog.setCancelable(cancelable);
            mLoadingDialog.setCanceledOnTouchOutside(cancelable);
            mLoadingDialog.show();
        }
    }

    /**
     * 隐藏加载等待框
     */
    protected void dismissProgressDialog() {
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
            mLoadingDialog = null;
        }
    }
}
